package com.saltedfish.tool.ui.calendar

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.saltedfish.tool.R
import com.saltedfish.tool.model.DayTable

class CalendarAdapter(lists: List<DayTable>) :
    BaseQuickAdapter<DayTable, BaseViewHolder>(
        R.layout.item_calendar,
        lists
    ) {

    override fun convert(helper: BaseViewHolder?, item: DayTable) {
        var flagContent = helper?.getView<TextView>(R.id.flagContent)
        var mViewLine = helper?.getView<View>(R.id.mViewLine)
        var iv_calendar_line = helper?.getView<View>(R.id.iv_calendar_line)

        var iv_select_calendar = helper?.getView<ImageView>(R.id.iv_select_calendar)

        var  str = "     "
        flagContent?.text = item.repeatTime +str+ item.title

        if(item.isTick){
            mViewLine?.visibility = View.VISIBLE
            flagContent?.setTextColor(Color.parseColor( "#CCCCCC"))

            var  spec = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)
            flagContent?.measure(spec,spec)
            var measuredWidth = flagContent?.measuredWidth
            var lp = mViewLine?.layoutParams
            if (measuredWidth != null) {
                if(item.title.length > 9){
                    lp?.width = measuredWidth
                }else{
                    lp?.width = measuredWidth + 40
                }
            }
            mViewLine?.layoutParams = lp
            iv_calendar_line?.visibility = View.VISIBLE
            iv_select_calendar?.setImageResource(R.drawable.date_button_finished)
        }else{
            flagContent?.setTextColor(Color.parseColor( "#333333"))
            iv_calendar_line?.visibility = View.INVISIBLE
            mViewLine?.visibility = View.INVISIBLE
            iv_select_calendar?.setImageResource(R.drawable.date_button_normal)

        }

        iv_select_calendar?.setOnClickListener {
            item.isTick = !item.isTick
            notifyDataSetChanged()
            if (calendarListener !=null){
                calendarListener!!.selectCalendar(item)
            }
        }

        flagContent?.setOnClickListener {
            if (calendarListener !=null){
                calendarListener!!.selectCalendarDate(item)
            }
        }

    }


    private var calendarListener: CalendarListener? = null

    interface CalendarListener {

        fun selectCalendar(item: DayTable)

        fun selectCalendarDate(item: DayTable)

    }


    fun setCalendarListener(listener: CalendarListener) {
        this.calendarListener = listener
    }


}